package com.hhs.storage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hhs.storage.bean.JxcMenu;
import com.hhs.storage.bean.JxcMenuRole;
import com.hhs.storage.bean.Result;
import com.hhs.storage.bean.TreeMenu;
import com.hhs.storage.service.JxcMenuRoleService;
import com.hhs.storage.service.JxcMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@RestController
@RequestMapping("do/menu")
@Slf4j
public class JxcMenuController {

    @Resource
    JxcMenuService jxcMenuService;


    /**
     * 用户登录后 ，初始化用户的菜单，该数据从session中获取，在shirorealm登录后存储
     * @return
     */
    @RequestMapping("init")
    public List<JxcMenu> menu() {
        List<JxcMenu> menulist = (List<JxcMenu>) SecurityUtils.getSubject().getSession().getAttribute("menulist");
        return menulist;
    }

    @RequestMapping("getAll")
    public List<TreeMenu> getAll() {
        List<JxcMenu> list = jxcMenuService.selectList(new EntityWrapper<JxcMenu>().orderBy("pid_,order_"));
        return TreeMenu.formatJxcMenu(list);
    }

    @RequestMapping("edit")
    public Map<String, Object> edit(JxcMenu jxcMenu) {
        log.info("添加下级菜单");
        jxcMenuService.insertOrUpdate(jxcMenu);
        return Result.sucJsonResp(jxcMenu);

    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除菜单");
        return jxcMenuService.dele(id);

    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public Map<String, Object> findById(@PathVariable("id") String id) {
        log.info("根据id查询菜单信息");
        JxcMenu jxcMenu = jxcMenuService.selectOne(new EntityWrapper<JxcMenu>().eq("id_", id));
        return Result.sucJsonResp(jxcMenu);

    }
    @RequestMapping(value = "roleId/{id}", method = RequestMethod.GET)
    public Map<String, Object> getByRoleId(@PathVariable("id") String roleId) {
        log.info("根据角色id查询关联菜单");
        List<JxcMenu> list = jxcMenuService.findByRoleId(roleId);
        return Result.sucJsonResp(list);

    }

}

